#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2020/11/24 10:04

@author: aimee.mei
"""

import logging,traceback
from util.db_util import execute_query
import datetime


def get_report_daily_id(type,bu_code,username):
    """ 获取日报id

    :param type:  日报提交类型 1:未上报，未保存；2：未上报，已保存；3：已上报，已保存
    :param bu_code: 分行code
    :param username: 分行账号

    :return: 日报id
    """

    curr_time = datetime.datetime.now()
    time_str = curr_time.strftime("%Y-%m-%d") #获取当天时间，格式 2020-11-26

    if type == 1:
        results = execute_query(
            "SELECT * FROM `risk_daily_report` WHERE bu_code = '%s' and upu = '%s' and report_status =1 and save_status = 1  ORDER BY id DESC" % (
            bu_code, username))
    elif type == 2:
        results = execute_query("SELECT * FROM `risk_daily_report` WHERE bu_code = '%s' and upu = '%s'  and report_status =1 and save_status = 2 order by rand() limit 1" % (
            bu_code, username))
    else :
        results = execute_query(
        "SELECT * FROM `risk_daily_report` WHERE report_date = '%s' and bu_code = '%s' and upu = '%s' and report_status =2 and save_status = 2 " %(time_str,bu_code,username) )
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


def get_daily_report_ids(bu_code,username):
    """ 获取所有保存了预警分类的日报id

    :return: 日报id  list
    """

    results = execute_query(
        "SELECT * FROM `risk_daily_report` WHERE bu_code = '%s' and upu = '%s'  and report_status =1 and save_status = 2 ORDER BY id DESC" % ( bu_code, username))
    try:
        ids = [row[0] for row in results]
        return ','.join([str(id) for id in ids])
    except Exception as e:
        logging.error("get_report_ids: %s"%e)
        return ""




def get_daily_report_event_id(type):
    """ 获取日报中事件id

    :return: 日报事件id
    """

    if type ==1:
        results = execute_query(
        "SELECT * FROM `risk_daily_report_event` where daily_report_id = %s;" %(get_report_daily_id(1,'ncb_nn','automation_tester2')) )
    else:
        results = execute_query(
        "SELECT * FROM `risk_daily_report_event` where daily_report_id = %s;" % (get_report_daily_id(1, 'ncb_nn', 'automation_tester2')))

    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


